﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace FalseColorEditor
{
    public class MenuManager
    {
        #region Fields
        private ToolStripPanel toolStripPanel;
        private MenuStrip menuStrip;
        private List<MenuParent> menuParents = new List<MenuParent>();
        #endregion

        #region Properties
        public List<MenuParent> MenuParents
        {
            get { return menuParents; }
            set { menuParents = value; }
        }
        #endregion

        #region Constructor
        public MenuManager(ToolStripPanel toolStripPanel, MenuStrip menuStrip)
        {
            this.toolStripPanel = toolStripPanel;
            this.menuStrip = menuStrip;
        }
        #endregion

        #region Public Methods
        public void Add(MenuParent menuParent)
        {
            this.menuParents.Add(menuParent);
        }
        public void Refresh()
        {
            // TODO: Remove Menu Components

            foreach (MenuParent menuParent in this.menuParents)
            {
                this.menuStrip.Items.Add(menuParent.CreateMenuBarItem());
                this.toolStripPanel.Controls.Add(menuParent.CreateToolBar());
            }
        }
        #endregion
    }

    public abstract class MenuObject
    {
        #region Fields
        private string name;
        private string text;
        private Bitmap image;
        private EventHandler onClick;
        private bool showAsMenuBar = true;
        private bool showAsToolBar = true;
        #endregion

        #region Properties
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        public Bitmap Image
        {
            get { return image; }
            set { image = value; }
        }
        public EventHandler OnClick
        {
            get { return onClick; }
            set { onClick = value; }
        }
        public bool ShowAsMenuBar
        {
            get { return showAsMenuBar; }
            set { showAsMenuBar = value; }
        }
        public bool ShowAsToolBar
        {
            get { return showAsToolBar; }
            set { showAsToolBar = value; }
        }
        #endregion

        #region Abstract Methods
        public abstract ToolStripItem CreateMenuBarItem();
        public abstract ToolStripItem CreateToolBarItem();
        #endregion
    }

    public class MenuItem : MenuObject
    {
        #region Constructors
        public MenuItem()
        {
            this.Text = "";
            this.Image = null;
        }
        public MenuItem(string text, Bitmap image)
        {
            this.Text = text;
            this.Image = image;
        }
        #endregion

        #region Public Methods
        public override ToolStripItem CreateMenuBarItem()
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            item.Text = this.Text;
            item.Image = this.Image;
            item.Click += this.OnClick;
            item.Visible = this.ShowAsMenuBar;
            return item;
        }
        public override ToolStripItem CreateToolBarItem()
        {
            ToolStripButton item = new ToolStripButton();
            item.Text = this.Text;
            item.Image = this.Image;
            item.Click += this.OnClick;
            item.Visible = this.ShowAsToolBar;
            return item;
        }
        #endregion
    }

    public class MenuGroup : MenuItem
    {
        #region Fields
        private List<MenuItem> menuItems = new List<MenuItem>();
        #endregion

        #region Properties
        public List<MenuItem> MenuItems
        {
            get { return menuItems; }
            set { menuItems = value; }
        }
        #endregion

        #region Constructors
        public MenuGroup()
        {
            this.Text = "";
            this.Image = null;
        }
        public MenuGroup(string text, Bitmap image)
        {
            this.Text = text;
            this.Image = image;
        }
        #endregion

        #region Public Methods
        public void Add(MenuItem menuItem)
        {
            this.menuItems.Add(menuItem);
        }
        public override ToolStripItem CreateMenuBarItem()
        {
            List<ToolStripItem> children = new List<ToolStripItem>();
            foreach (MenuItem child in this.menuItems)
            {
                children.Add(child.CreateMenuBarItem());
            }
            ToolStripMenuItem item = new ToolStripMenuItem(this.Text, this.Image, children.ToArray());
            item.Click += this.OnClick;
            item.Name = this.Name;
            item.Visible = this.ShowAsMenuBar;
            return item;
        }
        public override ToolStripItem CreateToolBarItem()
        {
            List<ToolStripItem> children = new List<ToolStripItem>();
            foreach (MenuItem child in this.menuItems)
            {
                children.Add(child.CreateToolBarItem());
            }
            ToolStripSplitButton item = new ToolStripSplitButton(this.Text, this.Image, children.ToArray());
            item.Click += this.OnClick;
            item.Name = this.Name;
            item.Visible = this.ShowAsToolBar;
            return item;
        }
        #endregion
    }

    public class MenuParent : MenuGroup
    {
        #region Constructors
        public MenuParent()
        {
            this.Image = null;
            this.Text = "";
        }
        public MenuParent(string text)
        {
            this.Image = null;
            this.Text = text;
        }
        #endregion

        #region Public Methods
        public Control CreateToolBar()
        {
            List<ToolStripItem> children = new List<ToolStripItem>();
            foreach (MenuItem child in this.MenuItems)
            {
                children.Add(child.CreateToolBarItem());
            }
            ToolStrip item = new ToolStrip(children.ToArray());
            item.Name = this.Name;
            item.Text = this.Text;
            item.Visible = this.ShowAsToolBar;
            return item;
        }
        #endregion
    }

    public class MenuSeperator : MenuItem
    {
        #region Constructors
        public MenuSeperator()
        {
            this.Text = "";
            this.Image = null;
        }
        #endregion

        #region Public Methods
        public override ToolStripItem CreateMenuBarItem()
        {
            ToolStripSeparator item = new ToolStripSeparator();
            item.Visible = this.ShowAsMenuBar;
            return item;
        }
        public override ToolStripItem CreateToolBarItem()
        {
            ToolStripSeparator item = new ToolStripSeparator();
            item.Visible = this.ShowAsToolBar;
            return item;
        }
        #endregion
    }
}
